<?php
namespace Admin\Controller;
use Think\Controller;


class RoleController extends Controller {

    public function index(){
        $role_model = D('Role');
        $list = $role_model->getAll();
        $this->assign('roles', $list);
        $this->display();
    }

    public function add(){
        $this->display();
    }

    public function add_role(){
        if(IS_POST){
            $role_model = D('Role');
            $role = filterInput($_POST);
            $result = $role_model->addObj($role, 'name');
            if($result){
                $this->success('新增成功');
            }else{
                $this->error('新增失败');
            }
        }
    }

    public function edit(){
        $get_data = filterInput($_GET);
        if(!isset($get_data['id'])){
            echo 'no parm id';
            return;
        }
        $role_model = \gt_base::load_model('Role');
        $role = $role_model->queryObj($get_data['id'], 'id');
        $this->assign('role', $role);
        $this->display();
    }

    public function edit_role(){
        if(IS_POST){
            $post_data = filterInput($_POST);
            if(!isset($post_data['id'])){
                echo 'no parm id';
                return;
            }
            $role_model = \gt_base::load_model('Role');
            $result = $role_model->updateObj($post_data);
            if($result){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
    }

    public function del(){
        $data = filterInput($_GET);
        $id = $data['id'];
        if($id==''||!isset($id)){
            echo 'no parm';
            return;
        }
        $admin_model = D('Admin');
        $admin_list = $admin_model->queryField('role_id', $id);
        if(!empty($admin_list)){
            $this->error('该角色下有成员，请先删除使用该角色的管理员。');
            exit;
        }
        $role_model = D('Role');
        $num = $role_model->delete($id);
        if($num > 0){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
        exit;
    }

    public function get_menu_priv_json(){
		header("Content-type: text/html; charset=utf-8");
        $get_data = filterInput($_GET);
        if(!isset($get_data['role_id'])){
            echo 'no parm role_id';
            return;
        }
        $menu_model = D('Menu');
        $menu = $menu_model->get_easyui_menu_tree_all_json(0, $get_data['role_id']);
        echo json_encode($menu);
    }

    public function get_role_priv(){
        $get_data = filterInput($_GET);
        if(!isset($get_data['role_id'])){
            echo 'no parm role_id';
            return;
        }
        $role_id = $get_data['role_id'];
        $role_model = D('Role');
        $role_priv_model = D('RolePriv');
        $role = $role_model->queryObj($role_id, 'id');
        $role_priv = $role_priv_model->queryObj($role_id, 'role_id');
        $menu_model = D('Menu');
        $menu = $menu_model->get_easyui_menu_tree_all();
        $this->assign('menu', $menu);
        $this->assign('role', $role);
        $this->display('set_role_priv');
    }

    public function save_role_priv(){
        $post_data = filterInput($_POST);
        if(!isset($post_data['role_id'])){
            echo 'no parm role_id';
            return;
        }
        $role_priv_model = D('RolePriv');
        echo $role_priv_model->update_priv($post_data['role_id'], $post_data['data']);

    }

    /**
     * 设置栏目权限
     */
    public function set_role_cat_priv(){
        $data = filterInput($_GET);
        if(!isset($data['role_id'])){
            echo 'no parm role_id';
            return;
        }
        //取得所有栏目
        $nav_model = D('Nav');
        $nav_list = $nav_model->get_all();
        $this->assign('nav_list', $nav_list);
        $this->display('set_role_cat_priv');
    }

    public function add_role_cat_priv(){
        $data = filterInput($_GET);
        $role = $data['role_id'];
        $temp = $data['val'];
        $temp = explode(",",$temp);
        $nav_id = $temp[0];
        $opr = $temp[1];
        $nav_priv_model = D('NavPriv');
        $nav_priv_model->add_new_one($role, $nav_id, $opr);
    }

    public function del_role_cat_priv(){
        $data = filterInput($_GET);
        $role = $data['role_id'];
        $temp = $data['val'];
        $temp = explode(",",$temp);
        $nav_id = $temp[0];
        $opr = $temp[1];
        $nav_priv_model = D('NavPriv');
        $nav_priv_model->del_new_one($role, $nav_id, $opr);
    }
}